﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class OrderPipesBySpacing : HYExternalCommand
	{
		public override Result HYExecute(ref string TranName)
		{
			if ((int)base.Doc.ActiveView.ViewType == 3)
			{
				MessageBox.Show("立面视图下不能使用排列功能", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return Autodesk.Revit.UI.Result.Failed;
			}
			TranName = "水平管等间距排列";
			double spacing = 3000.0;
			OrderElemsBySpacing orderElemsBySpacing = new OrderElemsBySpacing(this.commandData);
			Result result;
			try
			{
				for (;;)
				{
					Element element = null;
					YJKLine hyline = orderElemsBySpacing.GetDatumLine(out element);
					hyline.MakeUnBound();
					FormGetPipeSpacing formGetPipeSpacing = new FormGetPipeSpacing();
					formGetPipeSpacing.ShowDialog();
					spacing = formGetPipeSpacing.PipeSpacing / 304.8;
					if (formGetPipeSpacing.DialogResult != DialogResult.OK)
					{
						break;
					}
					List<Element> elementsByRectangle = orderElemsBySpacing.GetElementsByRectangle(element, hyline);
					double num = 0.0;
					YJKLine hyline2 = null;
					if (element is Pipe)
					{
						YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(element as Pipe);
						hyline2 = new YJKLine(YJKRevitTools.SetZToZero(hypiping.StartConnector.Origin), YJKRevitTools.SetZToZero(hypiping.EndConnector.Origin));
						if (!hyline.IsLineOnLine(hyline2.Line))
						{
							hyline = hyline2;
						}
						num = hypiping.GetOuterDiameter();
					}
					else if (element is Duct)
					{
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(element as Duct);
						hyline2 = new YJKLine(YJKRevitTools.SetZToZero(hypiping2.StartConnector.Origin), YJKRevitTools.SetZToZero(hypiping2.EndConnector.Origin));
						if (!hyline.IsLineOnLine(hyline2.Line))
						{
							hyline = hyline2;
						}
						num = hypiping2.GetWeightOrDiameter();
					}
					Dictionary<Element, double> dictionary = new Dictionary<Element, double>();
					Dictionary<Element, double> dictionary2 = new Dictionary<Element, double>();
					if (elementsByRectangle.Count >= 0)
					{
						XYZ xyz = null;
						foreach (Element element2 in elementsByRectangle)
						{
							LocationCurve locationCurve = element2.Location as LocationCurve;
							XYZ xyz2 = new XYZ(locationCurve.Curve.GetEndPoint(0).X, locationCurve.Curve.GetEndPoint(0).Y, 0.0);
							XYZ xyz3 = new XYZ(locationCurve.Curve.GetEndPoint(1).X, locationCurve.Curve.GetEndPoint(1).Y, 0.0);
							Line otherLine = Line.CreateBound(xyz2, xyz3);
							if (!hyline.IsLineOnLine(otherLine))
							{
								xyz = hyline.GetVector(otherLine).Normalize();
								break;
							}
						}
						if (xyz != null)
						{
							foreach (Element element3 in elementsByRectangle)
							{
								LocationCurve locationCurve2 = element3.Location as LocationCurve;
								XYZ xyz4 = new XYZ(locationCurve2.Curve.GetEndPoint(0).X, locationCurve2.Curve.GetEndPoint(0).Y, 0.0);
								XYZ xyz5 = new XYZ(locationCurve2.Curve.GetEndPoint(1).X, locationCurve2.Curve.GetEndPoint(1).Y, 0.0);
								Line line = Line.CreateBound(xyz4, xyz5);
								if (!hyline.IsLineOnLine(line))
								{
									XYZ xyz6 = hyline.GetVector(line).Normalize();
									double distance = hyline.GetDistance(line.GetEndPoint(0));
									if (xyz6.IsAlmostEqualTo(xyz))
									{
										dictionary.Add(element3, distance);
									}
									else
									{
										dictionary2.Add(element3, distance);
									}
								}
							}
							int spacingType = formGetPipeSpacing.SpacingType;
							if (spacingType != 0)
							{
								if (spacingType == 1)
								{
									if (dictionary.Count > 0)
									{
										if (hyline2 != null && hyline.IsPointOnLine(hyline2.StartPoint) && hyline.IsPointOnLine(hyline2.EndPoint))
										{
											orderElemsBySpacing.PipeDiameter = num / 2.0;
										}
										orderElemsBySpacing.OrderByOutlineSpacing(dictionary, xyz, spacing, formGetPipeSpacing.BeCaclInsulationThickness());
										orderElemsBySpacing.PipeDiameter = 0.0;
									}
									if (dictionary2.Count > 0)
									{
										if (hyline2 != null && hyline.IsPointOnLine(hyline2.StartPoint) && hyline.IsPointOnLine(hyline2.EndPoint))
										{
											orderElemsBySpacing.PipeDiameter = num * 304.8 / 2.0 / 304.8;
										}
										orderElemsBySpacing.OrderByOutlineSpacing(dictionary2, -xyz, spacing, formGetPipeSpacing.BeCaclInsulationThickness());
									}
								}
							}
							else
							{
								if (dictionary.Count > 0)
								{
									orderElemsBySpacing.OrderByCentreSpacing(dictionary, xyz, spacing);
									orderElemsBySpacing.PipeDiameter = 0.0;
								}
								if (dictionary2.Count > 0)
								{
									orderElemsBySpacing.OrderByCentreSpacing(dictionary2, -xyz, spacing);
								}
							}
						}
					}
				}
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}
	}
}
